package com.enjoyor.soa.traffic.core.efoms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.efoms.domain.VideoRelationInfo;
import com.enjoyor.soa.traffic.core.efoms.mapper.IVideoRelationInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.service.IVideoRelationInfoService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@Service
@Transactional
@SuppressWarnings("rawtypes")
public class VideoRelationInfoService implements IVideoRelationInfoService{

	@Autowired
	private IVideoRelationInfoMapper videoRelationInfoMapper;
	
	@Autowired
	private IPubLogMapper pubLogMapper;
	
	@Override
	public Page getVideoRelationInfoPage(String videoRelationId, String videoGroupId,String key,Page page) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getSelectMap(videoRelationId, videoGroupId,key);
			page.setMapBean(pramT);
			return this.videoRelationInfoMapper.getIVideoRelationInfoPage(page);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public List<VideoRelationInfo> getVideoRelationInfoList(String videoRelationId, String videoGroupId,String key) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getSelectMap(videoRelationId, videoGroupId,key);
			return this.videoRelationInfoMapper.getIVideoRelationInfoList(pramT);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public VideoRelationInfo getVideoRelationInfo(String videoRelationId) {
		// TODO Auto-generated method stub
		try {
			return this.videoRelationInfoMapper.selectById(videoRelationId);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public ResultPojo insertVideoRelationInfo(List<VideoRelationInfo> videoRelationList,String videoGroupId, String userName) {
		// TODO Auto-generated method stub
		try {
			//this.videoRelationInfoMapper.deleteVideoRelationInfoByGroupId(videoGroupId);
			for(int i = 0;i<videoRelationList.size();i++){
				String videoRelationId = "R" + TimeHelper.getUniqueString();
				VideoRelationInfo videoRelationInfo = videoRelationList.get(i);
				videoRelationInfo.setVideoRelationId(videoRelationId);
				this.videoRelationInfoMapper.insertSelective(videoRelationInfo);
			}
			pubLogMapper.saveNoramlLogInfo(userName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
					String.format("关联视频分组操作成功!", new Object[] {null}),
					LogInfoDic.LOG_MODULAR_ENUM.EFOMS_CHECK_MANAGEMENT.getMessage());
			return new ResultPojo();
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public ResultPojo updateVideoRelationInfo(VideoRelationInfo videoRelationInfo, String userName) {
		// TODO Auto-generated method stub
		try {
			this.videoRelationInfoMapper.updateByIdSelective(videoRelationInfo);
			pubLogMapper.saveNoramlLogInfo(userName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
					String.format("更新关联操作成功!", new Object[] {videoRelationInfo.getVideoRelationId()}),
					LogInfoDic.LOG_MODULAR_ENUM.EFOMS_CHECK_MANAGEMENT.getMessage());
			return new ResultPojo(videoRelationInfo.getVideoRelationId());
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public ResultPojo deleteVideoRelationInfo(String videoGroupId, String userName) {
		// TODO Auto-generated method stub
		try {
			this.videoRelationInfoMapper.deleteById(videoGroupId);
			pubLogMapper.saveNoramlLogInfo(userName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
					String.format("删除关联操作成功!", new Object[] {videoGroupId}),
					LogInfoDic.LOG_MODULAR_ENUM.EFOMS_CHECK_MANAGEMENT.getMessage());
			return new ResultPojo(videoGroupId);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	@Override
	public ResultPojo deleteVideoReletionByGroupIdAndVideoId(List<VideoRelationInfo> listVideoRelationInfo,String userName) {
		// TODO Auto-generated method stub
		for(int i = 0;i<listVideoRelationInfo.size();i++){
			Map<String, Object> map = new HashMap<>();
			map.put("videoGroupId",listVideoRelationInfo.get(i).getVideoGroupId());
			map.put("videoId", listVideoRelationInfo.get(i).getVideoId());
			this.videoRelationInfoMapper.deleteVideoReletionByGroupIdAndVideoId(map);
		}
		pubLogMapper.saveNoramlLogInfo(userName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
				String.format("批量删除操作成功!", new Object[] {null}),
				LogInfoDic.LOG_MODULAR_ENUM.EFOMS_CHECK_MANAGEMENT.getMessage());
		return new ResultPojo();
	}
	
	//------------------------------------私有方法开始---------------------------------------------//
	private Map<String, Object> getSelectMap(String videoRelationId, String videoGroupId,String key){
		Map<String, Object> map = new HashMap<>();
		map.put("videoRelationId", videoRelationId);
		map.put("videoGroupId", videoGroupId);
		map.put("key", key);
		return map;
	}

}
